package com.jy.rhin.valid;


import com.jy.rhin.infra.cda.XValid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
/**
 * @Author 姚俊
 * @Date 2023/2/28 17:04
 * @Description 时间校验器 dt15 兼容T格式
 */
@Component
@Slf4j
public class DT15Valid extends AbsValid {

    @Resource
    DT8Valid dt8Valid;
    @Resource
    DT6Valid dt6Valid;

    @Override
    public String validate(XValid xValid, String value) {
        String msg = "不符合格式" + xValid.dataElement().getDataElement().getFormat();
        String datetime = value;
        if(datetime.length() == 8){ //D8
            msg = dt8Valid.validate(xValid, datetime);
        }else if(datetime.length() == 6) { //D6
            msg = dt8Valid.validate(xValid, datetime);
        }else {
            if(datetime.length() == 15 && datetime.contains("T")){  //标准15位 yyyyMMddTHHMMSS 去除掉T
                datetime = datetime.replace("T", "");
            }
            if(datetime.length() == 14){
                msg = dt8Valid.validate(xValid, datetime.substring(0, 8));
                if (StringUtils.isEmpty(msg)) {
                    msg = dt6Valid.validate(xValid, datetime.substring(8));
                }
            }
        }
        return msg;
    }
}
